package com.block.job.service;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import com.block.common.service.ISourceDataCountInfoService;
import com.block.common.service.LogFactory;
import com.block.db.entity.SourceMemberDataInfo;
import com.block.db.mapper.MerchantDataPushInfoMapper;
import com.block.db.mapper.SourceMemberDataInfoMapper;
import com.block.source.service.ISourceMemberDataInfoService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@Component
public class UpdateSourceDataIpInfoJob {

    private static final Logger log = LogFactory.getJobLog();

    @Resource
    SourceMemberDataInfoMapper sourceMemberDataInfoMapper;
    @Autowired
    ISourceMemberDataInfoService sourceMemberDataInfoService;

    /**
     * 删除3天前的 机构撞库以及初始化数据
     * @throws Exception
     */
    @XxlJob("updateSourceDataIpInfoJob")
    public void delete() throws Exception {
        XxlJobHelper.log("更新当天线索数据的ip 信息---------开始执行--------");

        String dateInfo = XxlJobHelper.getJobParam();

        if (StrUtil.isEmpty(dateInfo)){
            dateInfo = LocalDate.now().toString();
        }

        List<SourceMemberDataInfo> dataList = sourceMemberDataInfoMapper.getIpInfoEmptyDataListByDateInfo(dateInfo);

        for (SourceMemberDataInfo memberDataInfo : dataList) {
            sourceMemberDataInfoService.updateIpInfo(memberDataInfo.getId());
            ThreadUtil.safeSleep(500);
        }

        String message = StrUtil.format("更新【dataList.size】{} ", dataList.size());
        XxlJobHelper.handleSuccess(message);

        XxlJobHelper.log("更新当天线索数据的ip 信息---------结束执行--------"+ message);
    }

}
